package com.web;

import com.domain.Employee;
import com.service.employeeService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
/**
 * 修改员工个人数据
 * 接收 updateEmployee.jsp 页面的表单数据
 * 提交到 employeeService 的 updateEmployee 函数
 * 重定向到 /findEmployee
 */
@WebServlet("/updateEmployee")
public class updateEmployeeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("你已经来到 /updateEmployee");
        //1.解决乱码问题
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        //2.接收表单数据
        Map<String, String[]> map = request.getParameterMap();
        //3.进行封装
        Employee employee = new Employee();
        try {
            BeanUtils.populate(employee,map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("正在修改数据 员工名为" + employee.getEmployeeName());
        //4.进行存储
        Integer integer = new employeeService().updateEmployee(employee);
        System.out.println(integer==1?"修改成功":"修改失败");
        //5.重定向到用户个人信息展示页面
        response.sendRedirect(request.getContextPath()+"/findEmployee?EmployeeName="+employee.getEmployeeName());
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
